#include "cmd_user.h"

cmd_user::cmd_user(){
}

cmd_user::cmd_user(User * user):user(user){
}

string cmd_user::query(){
    stringstream ss;
    interaction = new Interaction();
    userLogged = interaction->getUser();

    switch(cmd){
        case ALTERARNOME:
        case ALTERARENDERECO:
        case ALTERARSENHA:
        case APAGARUSUARIO:
        default:
            if(user == 0)
                throw("Usuario invalido!");
            break;
    }
    switch(cmd){
        case ALTERARNOME:
            ss << "UPDATE tb_users SET name="
               << "'" << user->getName().get() << "'"
               << " WHERE cpf='" << userLogged->getCPF().get() << "'"
               << ";";
            break;
        case ALTERARENDERECO:
            ss << "UPDATE tb_users SET address="
               << "'" << user->getAddress().get() << "'"
               << " WHERE cpf='" << userLogged->getCPF().get() << "'"
               << ";";
            break;
        case ALTERARSENHA:
            ss << "UPDATE tb_users SET password="
               << "'" << user->getPassword().get() << "'"
               << " WHERE cpf='" << userLogged->getCPF().get() << "'"
               << ";";
            break;
        case APAGARUSUARIO:
            ss << "DELETE FROM tb_users WHERE cpf='"
               << userLogged->getCPF().get() << "'"
               << ";";
            break;
    }
    return ss.str();
}

void cmd_user::setUser(User * user){
    this->user = user;
}

User * cmd_user::getUser(){
    return user;
}

void cmd_user::setCmd(int cmd){
    this->cmd = cmd;
}

/*****************************************************************/

cmd_auth::cmd_auth(){
}

cmd_auth::cmd_auth(User * user):user(user){
}

string cmd_auth::query(){
    stringstream ss;

    const float TOTAL_SALES_INITIAL = 0.;

    switch(cmd){
        case FAZERLOGIN:
        case CADASTRAR:
        default:
            if(user == 0)
                throw("Usuario invalido!");
            break;
    }
    switch(cmd){
        case FAZERLOGIN:
            ss << "SELECT * FROM tb_users WHERE cpf="
               << "'" << user->getCPF().get() << "'"
               << " AND password="
               << "'" << user->getPassword().get() << "'"
               << ";";
            break;
        case CADASTRAR:
            ss << "INSERT INTO tb_users VALUES ("
               << "'" << user->getName().get() << "', "
               << "'" << user->getPassword().get() << "', "
               << "\"" << user->getAddress().get() << "\", "
               << "'" << user->getCPF().get() << "', "
               << TOTAL_SALES_INITIAL << ")"
               << ";";
            break;
    }
    return ss.str();
}

void cmd_auth::setUser(User * user){
    this->user = user;
}

void cmd_auth::setCmd(int cmd){
    this->cmd = cmd;
}
